﻿@using Apps.Web.Core;
@using Apps.Common;
@using Apps.Models.Sys;
@using Apps.Locale;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index_Layout.cshtml";
    List<permModel> perm = null;
}
 
<table style="width: 100%">
    <tr>
        <td style="width: 600px;vertical-align: top">
            <div class="mvctool" style="width:600px;">
                <input id="txtQuery" type="text" class="searchText" placeholder="流程名称" style="width:100px;" />
                @Html.ToolButton("btnQuery", "fa fa-search", Resource.Query, ref perm, "Query", true)
                @Html.ToolButton("btnCreate", "fa fa-plus", "创建", ref perm, "Create", true)
                @Html.ToolButton("btnEdit", "fa fa-pencil", "修改", ref perm, "Edit", true) 
                @*@Html.ToolButton("btnEditLayout", "fa fa-list", "表单布局", ref perm, "Edit", true)*@
                @Html.ToolButton("btnStart", "fa fa-toggle-on", "启用", ref perm, "Edit", true)
                @Html.ToolButton("btnStop", "fa fa-toggle-off", "禁用", ref perm, "Edit", true)
                @Html.ToolButton("btnDelete", "fa fa-trash", Resource.Delete, ref perm, "Delete", true)
            </div>
            <div title="流程模板操作" class="easyui-panel"  >
                <table id="FlowList"  ></table>
            </div> 
        </td>
        <td style="width:3px;"></td>
        <td style="vertical-align: top">
            <div class="mvctool" >
               @Html.ToolButton("btnEditFlowStep", "fa fa-plus", "新增", ref perm, "Edit", true)  
               @Html.ToolButton("btnEditStep", "fa fa-pencil", "修改", ref perm, "Edit", true)
               @*@Html.ToolButton("btnEditStepRule", "fa fa-list", "设计分支",ref perm, "Edit", true)*@ 
               @Html.ToolButton("btnStepDelete", "fa fa-trash", Resource.Delete, ref perm, "Delete", true)
               @Html.ToolButton("btnDetails", "fa fa-list", "流程详细", ref perm, "Details", true)
            </div>
            <div title="流程步骤操作" class="easyui-panel"   >
                <table id="StepList"></table>
            </div>  
        </td> 
    </tr>
</table>

<div id="modalwindow" class="easyui-window" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
@Html.Partial("~/Views/Shared/_Partial_AutoGrid.cshtml")

<script type="text/javascript">
    var flowid;
    $(function () {
        function FlowRuleFormatter(value, check) {
            var checktype = '任一';
            if (check) checktype = '会签';
            switch (value) {
                case 1:
                    return "<span class='label label-success'>上级( " + checktype + " )</span>";
                    break;
                case 2:
                    return "<span class='label label-success'>人员( " + checktype + " )</span>";
                    break;
                case 3:
                    return "<span class='label label-success'>指派( " + checktype + " )</span>";
                    break;
                case 4:
                    return "<span class='label label-success'>职位( " + checktype + " )</span>";
                    break;
                case 5:
                    return "<span class='label label-success'>部门( " + checktype + " )</span>";
                    break;
                default:
                    return "<span class='label label-success'>申请( " + checktype + " )</span>";
                    break;
            }
        }

        $('#StepList').datagrid({
            url: '@Url.Action("GetStepList")',
            width: SetGridWidthSub(630),
            methord: 'post',
            height: SetGridHeightSub(90),
            fitColumns: true,
            sortName: 'Sort',
            sortOrder: 'asc',
            idField: 'Id',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: true,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            rownumbers: false,//行号
            //title: '流程步骤操作',
            columns: [[
                { field: 'StepNo', title: '步骤', width: 30, sortable: false },
                { field: 'Id', title: '', width: 80, hidden: true, sortable: false },
                { field: 'Name', title: '步骤名称', width: 70, sortable: false },
                { field: 'Remark', title: '步骤说明', width: 200, sortable: false, hidden: true },
                { field: 'FormId', title: '所属表单', width: 80, sortable: false, hidden: true },
                { field: 'FlowRule', title: '流转规则', width: 40, sortable: false, formatter: function (value, row) { return FlowRuleFormatter(value, row.IsAllCheck) } },
                { field: 'Execution', title: '审批人', width: 50, sortable: false, hidden: false },
                //{ field: 'Action', title: '分支', align: 'center', width: 30, sortable: false, hidden: false },
                { field: 'Sort', title: '@Resource.TitleSort', width: 20, sortable: false, hidden: true },
            ]]
        });

        $('#FlowList').datagrid({
            url: '@Url.Action("GetList")',
            width: 600,
            methord: 'post',
            height:  SetGridHeightSub(90),
            fitColumns: true,
            sortName: 'Id',
            sortOrder: 'desc',
            idField: 'Id',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: true,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            rownumbers: true,//行号
            //title: '流程模板操作',
            onLoadSuccess: function (data) {
                var allRows = $('#FlowList').datagrid('getRows');
                if (allRows.length > 0) {
                    $('#FlowList').datagrid('selectRow', 0);
                    flowid = allRows[0].Id;
                    $('#StepList').datagrid({ url: '@Url.Action("GetStepList")?id=' + flowid });
                    if (!allRows[0].State) {
                        $('#btnStart').hide();
                        $('#btnStop').show();
                        $('#btnEdit').hide();
                        $('#btnDelete').hide();
                        $('#btnEditFlowStep').hide();
                        $('#btnEditStep').hide();
                        //$('#btnEditStepRule').hide();
                        $('#btnStepDelete').hide();
                    }
                    else {
                        $('#btnStart').show();
                        $('#btnStop').hide();
                        $('#btnEdit').show();
                        $('#btnDelete').show();
                        $('#btnEditFlowStep').show();
                        $('#btnEditStep').show();
                        //$('#btnEditStepRule').show();
                        $('#btnStepDelete').show();
                    }
                }
            },
            columns: [[
                { field: 'Id', title: 'ID', width: 80, hidden: true },
                { field: 'Name', title: '流程名称', width: 80, sortable: true },
                { field: 'TypeName', title: '流程分类', width: 80, sortable: true, hidden: false },
                { field: 'Remark', title: '备注', width: 180, sortable: false, hidden: true },
                { field: 'UsingDep', title: '使用部门', width: 60, sortable: true , hidden: true},
                { field: 'IsExternal', title: '外部来源', width: 40, align: 'center', hidden: false, formatter: function (value) { return CustomFormatter(value, "是", "否") } },
                { field: 'ExternalURL', title: '来源地址', width: 180, sortable: true , hidden: true},

                { field: 'AttrA', title: '可用程度', width: 40, sortable: true , align: 'center', formatter: function (value,row) { if(value!=null || row.IsExternal==true) return EnableFormatterMes(true,"已完成"); else return EnableFormatterMes(false,"不完整") } },
                { field: 'State', title: '是否启用', width: 40, align: 'center', formatter: function (value) { return EnableFormatter(value) } },
                { field: 'CreateTime', title: '创建时间', width: 90, sortable: true, hidden: true }
            ]],
            onClickRow: function (index, data) {
                flowid = data.Id;
                $('#StepList').datagrid({ url: '@Url.Action("GetStepList")?id=' + flowid });

                if (!data.State)
                {
                    $('#btnStart').hide();
                    $('#btnStop').show();
                    $('#btnEdit').hide();
                    $('#btnDelete').hide();
                    $('#btnEditFlowStep').hide();
                    $('#btnEditStep').hide();
                    //$('#btnEditStepRule').hide();
                    $('#btnStepDelete').hide();
                }
                else
                {
                    $('#btnStart').show();
                    $('#btnStop').hide();
                    $('#btnEdit').show();
                    $('#btnDelete').show();
                    $('#btnEditFlowStep').show();
                    $('#btnEditStep').show();
                    //$('#btnEditStepRule').show();
                    $('#btnStepDelete').show();
                }

            }
        });
    });


    //ifram 返回
    function frameReturnByClose() {
        $("#modalwindow").window('close');
    }
    function frameReturnByReload(grid, flag) {
        if (flag)
            $("#" + grid).datagrid('load');
        else
            $("#" + grid).datagrid('reload');
    }
    function frameReturnByMes(mes) {
        $.messageBox5s(Lang.Tip, mes);
    }

    function SetRule(stepId, formid) {
        $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='no' frameborder='0'' src='/Flow/Form/StepRuleList?stepId=" + stepId + "&formId=" + formid + "'></iframe>");
        $("#modalwindow").window({ title: '设置分支', width: 900, height: 500, iconCls: 'fa fa-plus' }).window('open');
    }

    $(function () {

        $("#btnCreate").click(function () {
            $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='auto' frameborder='0'' src='@Url.Action("Create")'></iframe>");
            $("#modalwindow").window({ title: '设计流程模板', width: 500, height:530, iconCls: 'fa fa-plus' }).window('open');
        });
        @*$("#btnEditLayout").click(function () {
            var row = $('#FlowList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%'  frameborder='0' src='@Url.Action("FormLayout")?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
                $("#modalwindow").window({ title: '编辑表单布局', width: 800, height: 548, fit:true, iconCls: 'fa fa-pencil' }).window('open');
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });*@
        $("#btnEdit").click(function () {
            var row = $('#FlowList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%'  frameborder='0' src='@Url.Action("Edit")?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
                $("#modalwindow").window({ title: '编辑流程模板', width: 500, height: 530, iconCls: 'fa fa-pencil' }).window('open');
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });
        $("#btnEditStep").click(function () {
            var row = $('#StepList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%'  frameborder='0' src='@Url.Action("EditStep")?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
                $("#modalwindow").window({ title: '修改流程步骤', width: 900, height: 500, iconCls: 'fa fa-pencil' }).window('open');
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });
        $("#btnEditFlowStep").click(function () {
            var row = $('#FlowList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%'  frameborder='0' src='@Url.Action("EditFlowStep")?formid=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
                $("#modalwindow").window({ title: '新增流程步骤', width: 900, height: 500, iconCls: 'fa fa-pencil' }).window('open');
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });
        @*$("#btnEditStepRule").click(function () {
            var row = $('#StepList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='no' frameborder='0'' src='/Flow/Form/StepRuleList?stepId=" + row.Id + "&formId=" + flowid + "'></iframe>");
                $("#modalwindow").window({ title: '设计步骤分支', width: 900, height: 500, iconCls: 'fa fa-plus' }).window('open');
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });*@
        $("#btnDetails").click(function () {
            var row = $('#FlowList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='yes' frameborder='0'' src='/Flow/Form/Details?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
                $("#modalwindow").window({ title: '流程详细', width: 600, height: 400, iconCls: 'fa fa-plus' }).window('open');
                //$.modalWindow('流程设计详细', '@Url.Action("Details")?id=' + row.Id + '&Ieguid=' + GetGuid(), 500, 448, 'fa fa-list');
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });
        $("#btnStepDelete").click(function () {
            var row = $('#StepList').datagrid('getSelected');
            if (row != null) {
                $.messager.confirm(index_lang_tip, '你要删除当前步骤及条件吗？', function (r) {
                    if (r) {
                        $.post("@Url.Action("DeleteStep")?id=" + row.Id, function (data) {//从数据库删除
                            if (data.type == 1)
                            {
                                $("#StepList").datagrid('load');
                            }
                            $.messageBox5s(Lang.Tip, data.message);
                        }, "json");

                    }
                });
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });

        $("#btnStart").click(function () {
            var row = $('#FlowList').datagrid('getSelected');
            if (row != null) {
                $.messager.confirm(index_lang_tip, '启用[' + row.Name + ']流程吗？', function (r) {
                    if (r) {
                        $.post("@Url.Action("SetFlowState")?id=" + row.Id + "&state=true", function (data) {//从数据库删除
                            if (data.type == 1)
                            {
                                $("#FlowList").datagrid('load');
                            }
                            $.messageBox5s(Lang.Tip, data.message);
                        }, "json");

                    }
                });
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });

        $("#btnStop").click(function () {
            var row = $('#FlowList').datagrid('getSelected');
            if (row != null) {
                $.messager.confirm(index_lang_tip, '禁用[' + row.Name + ']流程吗？', function (r) {
                    if (r) {
                        $.post("@Url.Action("SetFlowState")?id=" + row.Id + "&state=false", function (data) {//从数据库删除
                            if (data.type == 1)
                            {
                                $("#FlowList").datagrid('load');
                            }
                            $.messageBox5s(Lang.Tip, data.message);
                        }, "json");

                    }
                });
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });

        $("#btnQuery").click(function () {
            var queryStr = $("#txtQuery").val();
            if (queryStr == null) {
                queryStr = "%";
            }
            $("#FlowList").datagrid("load", { queryStr: queryStr });

        });

        $("#btnDelete").click(function () {
            dataDelete("@Url.Action("Delete")", "FlowList");
        });
    });
</script>
 
